<?php

namespace App\Services;

use App\Models\Crm_Follow_Template;
use App\Models\Crm_Member_Follow;
use App\Traits\External;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Throwable;

class FollowService
{
    public function getFollows($params)
    {
        $result = Crm_Member_Follow::getFilter($params)
            ->select(['id','mid','mode','intention','content','nexttime','imgs','optionid','optionname','created_at'])
            ->Nodel()
            ->get();
        foreach ($result as $key => $value) {
            $result[$key]['imgs'] = json_decode($value->imgs,true);
            $result[$key]['annex'] = $value->hasOneAnnex()->select(['id','content','optionid','optionname','created_at'])->Nodel()->first();
        }
        return $result;
    }

    public function getFollowTemplate($params)
    {
        return Crm_Follow_Template::getFilter($params)
            ->select(['id','title','text'])
            ->Nodel()
            ->get();
    }

    public function followTemplate($params)
    {
        $result = Crm_Follow_Template::getFilter($params)
            ->Nodel()
            ->paginate($params['pageSize']);
        $list['list'] = $result->items();
        $list['total'] = $result->total();
        return $list;
    }

    public function followTemplateInsert($params)
    {
        try {
            // 开始数据库事务
            DB::beginTransaction();
            $res = Crm_Follow_Template::create([
                'agencyid' => $params['aid'],
                'type' => $params['type'],
                'title' => $params['title'],
                'text' => $params['text']
            ]);
            if(!$res){
                throw new \Exception("添加跟进模板失败" );
            }
            DB::commit();
            return true;
        }catch (Throwable $e) {
            // 如果在事务执行过程中发生异常，则回滚事务
            DB::rollBack();
            // 记录日志或处理错误
            Log::error("Database transaction failed: " . $e->getMessage());
            return false;
        }
    }

    public function followTemplateUpdate($params)
    {
        try {
            // 开始数据库事务
            DB::beginTransaction();
            $res = Crm_Follow_Template::where('id',$params['id'])->update([
                'type' => $params['type'],
                'title' => $params['title'],
                'text' => $params['text']
            ]);
            if(!$res){
                throw new \Exception("更新跟进模板失败" );
            }
            DB::commit();
            return true;
        }catch (Throwable $e) {
            // 如果在事务执行过程中发生异常，则回滚事务
            DB::rollBack();
            // 记录日志或处理错误
            Log::error("Database transaction failed: " . $e->getMessage());
            return false;
        }
    }

    public function followTemplateDel($params)
    {
        try {
            // 开始数据库事务
            DB::beginTransaction();
            $res = Crm_Follow_Template::where('id',$params['id'])->update([
                'is_del' => 'Y'
            ]);
            if(!$res){
                throw new \Exception("删除跟进模板失败" );
            }
            DB::commit();
            return true;
        }catch (Throwable $e) {
            // 如果在事务执行过程中发生异常，则回滚事务
            DB::rollBack();
            // 记录日志或处理错误
            Log::error("Database transaction failed: " . $e->getMessage());
            return false;
        }
    }
}
